
package server;


/**
 * The tester for server socket connection. It sends PING packets and sample
 * data packets every 5 seconds and displays any packets received. <br>
 *
 * The class allows multiple clients by creating a thread for each.
 * <br>
 *
 * @author c8yanche
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final int PORT = 7777;
        final int TIME_INTERVAL = 5000;
        ServerListener testServer = new ServerListener(PORT);
        System.out.printf("listening on %d\n", PORT);
        //start ServerListener
        testServer.start();
        //a packet with two events, with new line char.
        String TwoDataPacketLn =
                "DATA" +
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<data>\n" +
                "    <event type=\"MS State\">\n" +
                "        <id>0x0001</id>\n" +
                "        <time>Oct 3, 2009 10:12:25 AM</time>\n" +
                "        <state>failed</state>\n" +
                "        <source>MS #1</source>\n" +
                "        <details>\n" +
                "            <strong>Analyst terminated.</strong><br />\n" +
                "            Error code: 0x0000036\n" +
                "        </details>\n" +
                "    </event>\n" +
                "    <event type=\"MS State\">\n" +
                "        <id>0x0002</id>\n" +
                "        <time>Oct 6, 2009 05:34:49 PM</time>\n" +
                "        <state>completed</state>\n" +
                "        <source>MS #2</source>\n" +
                "    </event>\n" +
                "</data>\n";
        //Sample with two events, without new line char.
        String TwoDataPacket =
                "DATA" +
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data><event type=\"MS State\"><id>0x0001</id>" +
                "<time>Oct 3, 2009 10:12:25 AM</time><state>failed</state>" +
                "<source>MS #1</source><details><strong>Analyst terminated.</strong><br />" +
                "Error code: 0x0000036</details></event><event type=\"MS State\">" +
                "<id>0x0002</id><time>Oct 6, 2009 05:34:49 PM</time><state>completed</state><source>" +
                "MS #2</source></event></data>";
        //sample emergency event
        String SingleDataPacket1 =
                "DATA" +
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                    "<event type=\"MS State\">" +
                        "<id>0x0001</id>" +
                        "<time>Oct 3, 2009 10:12:25 AM</time>" +
                        "<state>failed</state>" +
                        "<source>MS #1</source>" +
                        "<details>" +
                            "<strong>Analyst terminated.</strong><br />" +
                            "Error code: 0x0000036" +
                        "</details>" +
                    "</event>" +
                "</data>";
        //sample non-emergency event
        String SingleDataPacket2 =
                "DATA" +
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                    "<event type=\"MS State\">" +
                        "<id>0x0002</id>" +
                        "<time>Oct 6, 2009 05:34:49 PM</time>" +
                        "<state>completed</state>" +
                        "<source>MS #2</source>" +
                    "</event>" +
                "</data>";
        int k = 0;

            while (true){
            try {
                Thread.sleep(TIME_INTERVAL);
                //test ping
                testServer.broadcast("PING" + Integer.toString(k++));
                Thread.sleep(TIME_INTERVAL);
                //test non-emergency event
                testServer.broadcast(SingleDataPacket2);
                Thread.sleep(TIME_INTERVAL);
                //test emergency
                testServer.broadcast(SingleDataPacket1);
                Thread.sleep(TIME_INTERVAL);
                //testServer.broadcast("SERRserver error test1");

                //test simutaneous events
                testServer.broadcast(SingleDataPacket2);
                testServer.broadcast(SingleDataPacket1);
                Thread.sleep(TIME_INTERVAL);

                //test packets with multiple events
                testServer.broadcast(TwoDataPacket);
                Thread.sleep(TIME_INTERVAL);
                testServer.broadcast(TwoDataPacketLn);


            } catch (InterruptedException ex) {
                System.out.println("Interrupted.");
            }
            }

    }

}
